<template>
    <div>
        <group>
            <x-input title="手机号" v-model="login" placeholder="手机号码" type="tel"></x-input>
            <x-input title="密码" v-model="password" placeholder="密码" type="password"></x-input>
        </group>
        <group>
            <x-button @click.native="loginUser" type="primary">登录</x-button>
        </group>
        <toast v-model="show" type="cancel" text="手机号或密码错误"></toast>
    </div>
</template>
<script>
    import {XInput, Group, XButton, Cell, Toast} from 'vux'
export default {
    components: {
        XInput,
            XButton,
            Group,
            Cell,
            Toast
    },
    data() {
        return {
            login: "",
            password: "",
            show: false,
        }
    },
    created() {
        let token = localStorage.getItem("chuang_token")
        if (token != null) {
            this.$router.replace({path: "main", query: {type: "all", action: "login"}})
        }
    },
       methods: {
        loginUser(){
            let user = {
                login: this.login,
                password: this.password
            }
            this.$http.post("sign_in.json", user).then(res => {
                console.log(res)
                let data = res.data;
                localStorage.removeItem("chuang_token", data.token)
                localStorage.setItem("chuang_token", data.token)
                localStorage.setItem("chuang_user", JSON.stringify(data.user))
                window.location.reload()
            })
        }
    }
}
</script>
